package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;
import android.widget.Toast;

import com.jiangxiaofei.citycloud.bean.FleaGoodBean;
import com.jiangxiaofei.citycloud.bean.GoodDetailBean;
import com.jiangxiaofei.citycloud.bean.GoodListBean;
import com.jiangxiaofei.citycloud.bean.FleaMenuBean;
import com.jiangxiaofei.citycloud.bean.NeedBean;
import com.jiangxiaofei.citycloud.bean.GoodIdBean;
import com.jiangxiaofei.citycloud.httpUtil.HttpResult;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.httpUtil.RetrofitUtils;
import com.jiangxiaofei.citycloud.param.FleaChildGoodsParam;
import com.jiangxiaofei.citycloud.param.FleaGetDetailParam;
import com.jiangxiaofei.citycloud.param.FleaGoodListParam;
import com.jiangxiaofei.citycloud.param.GoodsPublicParam;
import com.jiangxiaofei.citycloud.param.FleaNeedListParam;
import com.jiangxiaofei.citycloud.param.PutNeedParam;
import com.jiangxiaofei.citycloud.param.SearchParam;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: JiangXFei
 * @date: 2017/11/9 0009
 * @content:
 */

public class FleaModel implements FleaConstant.GetFleaMenuMobel, FleaConstant.PutFleaGoodsModel
        , FleaConstant.GetNewGoodsModel, FleaConstant.GetGoodDetailModel, FleaConstant.GetChildNewGoodsModel
        , FleaConstant.GetGoodListModel, FleaConstant.PutNeedModel, FleaConstant.GetNeedDetailModel
        , FleaConstant.GetNeedListModel, FleaConstant.SearchGoodModel, FleaConstant.SearchNeedModel {

    private void toast(Context mContext, String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 获取二手物品分类
     *
     * @param context
     * @param id
     * @param callback
     */

    /**
     * 获取所有菜单
     *
     * @param context
     * @param callback
     */
    @Override
    public void getMainFleaMenu(Context context, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(context)
                .getFleaMainMenu(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FleaMenuBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onFailure("", 0);
                    }

                    @Override
                    public void onNext(HttpResult<List<FleaMenuBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            toast(context, listHttpResult.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取二级页面
     *
     * @param context
     * @param id
     * @param callback
     */
    @Override
    public void getSencendMenu(Context context, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(context)
                .getFleaSencendMenu(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FleaMenuBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onFailure("", 0);
                    }

                    @Override
                    public void onNext(HttpResult<List<FleaMenuBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            toast(context, listHttpResult.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取三级页面
     *
     * @param context
     * @param id
     * @param callback
     */
    @Override
    public void getThreeMenu(Context context, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(context)
                .getFleaThreeMenu(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FleaMenuBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onFailure("", 0);
                    }

                    @Override
                    public void onNext(HttpResult<List<FleaMenuBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            toast(context, listHttpResult.getMessage());
                        }
                    }
                });
    }

    /**
     * 发布二手商品
     */
    @Override
    public void putFleaGoods(Context mContext, GoodsPublicParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext).putFleaGoods(param.getTitle(), param.getDescrption(),
                param.getPrice(), param.getCat(), param.getLat(),
                param.getLng(), param.getAuthor(), param.getImg())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodIdBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        toast(mContext, "网络异常，请稍后再试！");
                    }

                    @Override
                    public void onNext(HttpResult<List<GoodIdBean>> objectHttpResult) {
                        if (objectHttpResult.getCode() == 1) {
                            callback.onSuccess(objectHttpResult.getData(), objectHttpResult.getMessage(), objectHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取最新发布商品
     *
     * @param mContext
     * @param page
     * @param callback
     */
    @Override
    public void getNewGoods(Context mContext, int page, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getHomeGoodList(page)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FleaGoodBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, "没有数据了", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(HttpResult<List<FleaGoodBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, listHttpResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * 获取商品详情页
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getGoodDetail(Context mContext, FleaGetDetailParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getGoodDetail(param.getId(), param.getLng(), param.getLat())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodDetailBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, "网络异常,请稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(HttpResult<List<GoodDetailBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, listHttpResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * 获取二级页面下商品信息
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getChildNewGoods(Context mContext, FleaChildGoodsParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getChildNewGoods(param.getId(), param.getPage())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FleaGoodBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, "没有数据了", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(HttpResult<List<FleaGoodBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, listHttpResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * 获取商品列表页
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getGoodList(Context mContext, FleaGoodListParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFleaGoodList(param.getId(), param.getPage(), param.getLat(), param.getLng(),
                        param.getIssue(), param.getPrice(), param.getDis())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodListBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodListBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, listHttpResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * 发布需求
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void putNeed(Context mContext, PutNeedParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .putFleaNeeds(param.getTitle(), param.getDescription()
                        , param.getLat(), param.getLng(), param.getPrice(), param.getCat(), param.getAuthor())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodIdBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        toast(mContext, "网络异常，请稍后再试！");
                    }

                    @Override
                    public void onNext(HttpResult<List<GoodIdBean>> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            callback.onFailure(stringHttpResult.getMessage(), stringHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取需求详情
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getNeedDetail(Context mContext, FleaGetDetailParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getNeedDetail(param.getId(), param.getLng(), param.getLat())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<NeedBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        toast(mContext, "网络异常！");
                    }

                    @Override
                    public void onNext(HttpResult<List<NeedBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取需求列表
     */
    @Override
    public void getNeedList(Context mContext, FleaNeedListParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getNeedList(param.getId(), param.getPage(), param.getLat(), param.getLng(), param.getIssue())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<NeedBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<NeedBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 搜索商品
     */
    @Override
    public void searchGood(Context mContext, SearchParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .searchGoods(param.getPage(), param.getKey(), param.getLat(), param.getLng())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodListBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodListBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 搜索需求
     */
    @Override
    public void searchNeed(Context mContext, SearchParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .searchNeeds(param.getPage(), param.getKey(), param.getLat(), param.getLng())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<NeedBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<NeedBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }
}
